//
//  AlivcRecordBottomView.m
//  AlivcVideoClient_Entrance
//
//  Created by wanghao on 2019/2/25.
//  Copyright © 2019年 Alibaba. All rights reserved.
//

#import "AlivcRecordBottomView.h"
#import "AlivcRecordRateSelectView.h"
#import "AlivcRecordUIConfig.h"
#import "AlivcRecordButtonView.h"
#import "AlivcButton.h"
#import "NSString+AlivcHelper.h"

@interface AlivcRecordBottomView()<AlivcRecordButtonViewDelegate>

/// 相册按钮
@property(nonatomic, strong)AlivcButton *photoAlbumBtn;

/// 道具按钮
@property(nonatomic, strong)AlivcButton *pasterBtn;

/// 回删button
@property(nonatomic, strong) UIButton *deleteButton;

@property(nonatomic, strong)AlivcRecordButtonView *recordButttonView;



@property(nonatomic, strong)AlivcRecordRateSelectView *rateSelectView;

@end


@implementation AlivcRecordBottomView

- (instancetype)initWithFrame:(CGRect)frame withUIConfig:(nonnull AlivcRecordUIConfig *)uiConfig{
    self =[super initWithFrame:frame];
    if (self) {
        [self setupSubviewsWithUIConfig:uiConfig];
    }
    return self;
}

- (void)setupSubviewsWithUIConfig:(AlivcRecordUIConfig *)uiConfig{
    //速度选择条
    _rateSelectView =[[AlivcRecordRateSelectView alloc]initWithFrame:CGRectMake(30, 0, ScreenWidth-60, 40)];
    __weak typeof(self)weakSelf = self;
    [_rateSelectView setupViewsWithItems:@[[@"极慢" localString],[@"慢" localString],[@"标准" localString],[@"快" localString],[@"极快" localString]] selectedRateBlock:^(CGFloat rate) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(alivcRecordBottomViewDidSelectRate:)]) {
            [weakSelf.delegate alivcRecordBottomViewDidSelectRate:rate];
        }
    }];
    [self addSubview:_rateSelectView];
    

    
    //美颜按钮
    CGFloat btn_size =31;
    CGFloat center_y =CGRectGetHeight(self.frame)- 50- btn_size/2 -30;
    _photoAlbumBtn =[[AlivcButton alloc]initWithButtonType:AlivcButtonTypeTitleBottom];
    [_photoAlbumBtn setTitle:[@"相册" localString] forState:UIControlStateNormal];
    _photoAlbumBtn.titleLabel.font =[UIFont systemFontOfSize:13];
    _photoAlbumBtn.frame =CGRectMake(CGRectGetMinX(_rateSelectView.frame)+40, center_y - btn_size/2, btn_size, btn_size+20);
    [_photoAlbumBtn setImage:UIImageMake(@"shortVideo_PhotoAlbum") forState:UIControlStateNormal];
    [_photoAlbumBtn addTarget:self action:@selector(photoAlbumAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_photoAlbumBtn];
    
    //删除button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:UIImageMake(@"shortVideo_Delete") forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    button.frame = CGRectMake(_rateSelectView.x + 35, center_y - 10, 40, 40);
    self.deleteButton = button;
    
    //特效按钮
    _pasterBtn =[[AlivcButton alloc]initWithButtonType:AlivcButtonTypeTitleBottom];
    _pasterBtn.frame =CGRectMake(CGRectGetWidth(self.frame)- CGRectGetMaxX(_photoAlbumBtn.frame), CGRectGetMinY(_photoAlbumBtn.frame), btn_size, CGRectGetHeight(_photoAlbumBtn.frame));
    [_pasterBtn setTitle:[@"道具" localString] forState:UIControlStateNormal];
    _pasterBtn.titleLabel.font =[UIFont systemFontOfSize:13];
    [_pasterBtn setImage:UIImageMake(@"shortVideo_gif") forState:UIControlStateNormal];
    [_pasterBtn addTarget:self action:@selector(effectBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_pasterBtn];
    
    //录制按钮View
    _recordButttonView = [[AlivcRecordButtonView alloc]initWithFrame:CGRectMake((CGRectGetWidth(self.frame)-120)/2, CGRectGetMidY(_photoAlbumBtn.frame)+40-130, 120, 130)];
    _recordButttonView.delegate = self;
    [self addSubview:_recordButttonView];
    [self sendSubviewToBack:_recordButttonView];


}

#pragma mark - Actions
- (void)updateUI{
    BOOL isRecording =[self isRecording];
    if (isRecording) {
        [_photoAlbumBtn setHidden:isRecording];
        self.deleteButton.hidden = isRecording;
    }
    [_pasterBtn setHidden:isRecording];
//    [_recordButttonView setRecordStatud:isRecording];
    [_recordButttonView setRecordStatus:isRecording withRecordType:0];
    [_rateSelectView setHidden:isRecording];
}
- (void)startRecordDelegate{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewStartRecord)]) {
        [self.delegate alivcRecordBottomViewStartRecord];
    }
}
- (void)stopRecordDelegate{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewStopRecord)]) {
        [self.delegate alivcRecordBottomViewStopRecord];
    }
}
- (BOOL)isRecording{
    DLog(@"isRecording");
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewIsRecording)]) {
        DLog(@"isRecordingAction");

        return [self.delegate alivcRecordBottomViewIsRecording];
    }
    return NO;
}
- (void)updateViewsWithVideoPartCount:(NSInteger)partCount{
    self.deleteButton.hidden = !(partCount>0);
    self.photoAlbumBtn.hidden = (partCount>0);
}

- (void)photoAlbumAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewphotoAlbumButtonOnclick)]) {
        [self.delegate alivcRecordBottomViewphotoAlbumButtonOnclick];
    }
}
- (void)beautyBtnAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewBeautyButtonOnclick)]) {
        [self.delegate alivcRecordBottomViewBeautyButtonOnclick];
    }
}
- (void)deleteBtnAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewDeleteVideoPart)]) {
        [self.delegate alivcRecordBottomViewDeleteVideoPart];
    }
}
- (void)effectBtnAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alivcRecordBottomViewEffectButtonOnclick)]) {
        [self.delegate alivcRecordBottomViewEffectButtonOnclick];
    }
}

#pragma mark - Public
- (void)refreshRecorderVideoDuration:(CGFloat)duration{
    [self.recordButttonView refreshRecordingTime:duration];
}
- (void)updateRecorderUI{
  
    [self updateUI];
}
- (void)startRecord{
    [self startRecordDelegate];
    [self updateUI];
}




#pragma mark - AlivcRecordButtonViewDelegate
- (void)alivcRecordButtonTouchUp{
    BOOL isRecording =[self isRecording];
    if (isRecording) {
        DLog(@"正在录制中停止录制");
        [self stopRecordDelegate];
    }
    else
    {
        DLog(@"没有在录制开始录制");
        [self startRecordDelegate];
    }
    [self updateUI];
}
- (void)alivcRecordButtonTouchDown{
    DLog(@"recordButtonTouchDown");

}
- (void)alivcRecordButtonTouchUpDragOutside{
    DLog(@"recordButtonTouchUpDragOutside");

}

@end
